package com.lct.simpleforecast.ui.adapter.base

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.lct.simpleforecast.extensions.ctx

/**
 * Author：SkySmile
 * Date：2018/7/4
 * Description：对RecyclerView.Adapter的封装
 */
abstract class MyBaseAdapter<T>(private val layoutId: Int,
                                private val dataList: List<T>)
    : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return ViewHolder(LayoutInflater
                .from(parent.ctx)
                .inflate(layoutId, parent, false))
    }

    override fun getItemCount(): Int = dataList.size

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        bindData(holder.itemView, dataList[position])
    }

    /**
     * 绑定数据
     *
     * @param   itemView item布局
     * @param   dataBean 数据对象
     */
    abstract fun bindData(itemView: View, dataBean: T)

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

}